﻿// <copyright file="TagGeneric.cs" company="Signal Processing Lab, Wuhan University">
// Copyright (c) 2009-2010 All Right Reserved
// </copyright>
// <author>Xinping Deng</author>
// <email>dentoby@gmail.com</email>
// <date>2010-4</date>
// <summary>This is part of Image.NET project.</summary>

namespace SPLWHU.Core
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The generic class of <see cref="Tag"/>.
    /// </summary>
    /// <typeparam name="T">The type of value of this tag.</typeparam>
    public class Tag<T> : Tag
    {
        /// <summary>
        /// The value of this tag.
        /// </summary>
        private T value;

        /// <summary>
        /// The previous value.
        /// </summary>
        private T preValue;

        /// <summary>
        /// Initializes a new instance of the <see cref="Tag&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        public Tag(string name)
        {
            this.Name = name;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Tag&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        /// <param name="value">The value to be set.</param>
        public Tag(string name, T value)
        {
            this.Name = name;
            this.value = value;
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        public virtual T Value
        {
            get
            {
                return this.value;
            }

            set
            {
                if (this.value == null || !this.value.Equals(value))
                {
                    // saves the new value.
                    this.preValue = value;
                    this.value = value;

                    // fires the event.
                    this.FireValueChanged(this.preValue, value);
                }
            }
        }

        /// <summary>
        /// Gets the type of value stored in this tag.
        /// </summary>
        /// <value></value>
        public override Type ValueType
        {
            get { return typeof(T); }
        }

        /// <summary>
        /// Resets the value to default value.
        /// </summary>
        public override void ResetValue()
        {
            this.value = this.preValue;
        }

        /// <summary>
        /// Retrives the value in this tag.
        /// </summary>
        /// <returns>The value stored in this tag.</returns>
        public override object GetValue()
        {
            return this.value;
        }

        /// <summary>
        /// Converts the value of this instance to text.
        /// </summary>
        /// <returns>Converted text representing the value.</returns>
        public override string ValueToText()
        {
            return TagValueParser.ConvertToString<T>(this.value);
        }

        /// <summary>
        /// Parses the value of this instance from text.
        /// </summary>
        /// <param name="text">The text to be parsed to generate value.</param>
        public override void ValueFromText(string text)
        {
            T val = TagValueParser.Parse<T>(text);
            if (!val.Equals(this.value))
            {
                this.preValue = this.value;
                this.value = val;
            }
        }
    }
}
